﻿/************************************************************************************
 * xujiajun@yoozoo.com 御剑
 * 供Lua端在运行时动态添加的离散分布对象层级，并支持监听LOD数据的变化
 ***********************************************************************************/

using Unity.Mathematics;
using UnityEngine;
using XLua;
using WorldMapBase.Layer;
using WorldMapBase.LOD;

namespace WorldMapBase
{
    /// <summary>
    /// 供Lua端在运行时动态添加的离散分布对象层级，并支持监听LOD数据的变化
    /// </summary>
    public class LuaDiscreteObjectLODLayer : DiscreteObjectLayer, IDiscreteObjectLayerListener, ILODListener
    {
        /// <summary>
        /// C#触发LOD等级变化时调用Lua的方法名。传参为(int currentLodLevel, int lastLodLevel)
        /// </summary>
        public static string FunctionName_OnLODLevelChange = "OnLODLevelChange";

        /// <summary>
        /// C#触发LOD百分比变化时调用Lua的方法名。传参为(int lodLevel, int nextLevelPercent)
        /// </summary>
        public static string FunctionName_OnNextLODPercentUpdate = "OnNextLODPercentUpdate";

        /// <summary>
        /// C#触发离散分布对象进出视野时调用Lua的方法名。传参为(int id, bool hideFlag)
        /// </summary>
        public static string FunctionName_OnObjectVisibleChange = "OnObjectVisibleChange";

        public static string FunctionName_OnUpdateViewRect = "OnUpdateViewRect";

        private LuaTable m_LuaTable;
        private LuaFunction m_LuaFunction_OnLODLevelChange;
        private LuaFunction m_LuaFunction_OnNextLODPercentUpdate;
        private LuaFunction m_LuaFunction_OnObjectVisibleChange;
        private LuaFunction m_LuaFunction_OnUpdateViewRect;

        internal LuaDiscreteObjectLODLayer(LuaTable luaTable, int gridWidth, int gridHeight) : base(gridWidth, gridHeight)
        {
            m_LuaTable = luaTable;
            m_LuaFunction_OnLODLevelChange = m_LuaTable.Get<LuaFunction>(FunctionName_OnLODLevelChange);
            m_LuaFunction_OnNextLODPercentUpdate = m_LuaTable.Get<LuaFunction>(FunctionName_OnNextLODPercentUpdate);
            m_LuaFunction_OnObjectVisibleChange = m_LuaTable.Get<LuaFunction>(FunctionName_OnObjectVisibleChange);
            m_LuaFunction_OnUpdateViewRect = m_LuaTable.Get<LuaFunction>(FunctionName_OnUpdateViewRect);
            SetListener(this);
        }

        public void OnLODLevelChange(int currentLodLevel, int lastLodLevel)
        {
            m_LuaFunction_OnLODLevelChange?.Call(currentLodLevel, lastLodLevel);
        }

        public void OnNextLODPercentUpdate(int lodLevel, float nextLevelPercent)
        {
            m_LuaFunction_OnNextLODPercentUpdate?.Call(lodLevel, nextLevelPercent);
        }


        protected override void OnUpdateViewRect(int4 viewRect)
        {
            //base.OnUpdateViewRect(viewRect);
            m_LuaFunction_OnUpdateViewRect?.Call(viewRect.x,viewRect.y,viewRect.z,viewRect.w);
        }

        public void OnObjectVisibleChange(string id, bool hideFlag)
        {
            m_LuaFunction_OnObjectVisibleChange?.Call(id, hideFlag);
        }
    }

}
